
// #/:core/query
sammy.get
(
  /^#\/([\w\d-]+)\/(query)$/,
  function( context )
  {
    var core_basepath = this.active_core.attr( 'data-basepath' );
    var content_element = $( '#content' );
        
    $.get
    (
      'tpl/query.html',
      function( template )
      {
        content_element
          .html( template );

        var query_element = $( '#query', content_element );
        var query_form = $( '#form form', query_element );
        var url_element = $( '#url', query_element );
        var result_element = $( '#result', query_element );
        var response_element = $( '#response iframe', result_element );

        url_element
          .die( 'change' )
          .live
          (
            'change',
            function( event )
            {
              var check_iframe_ready_state = function()
              {
                var iframe_element = response_element.get(0).contentWindow.document || response_element.get(0).document;

                if( !iframe_element )
                {
                  console.debug( 'no iframe_element found', response_element );
                  return false;
                }

                url_element
                  .addClass( 'loader' );

                if( 'complete' === iframe_element.readyState )
                {
                  url_element
                    .removeClass( 'loader' );
                }
                else
                {
                  window.setTimeout( check_iframe_ready_state, 100 );
                }
              }
              check_iframe_ready_state();

              response_element
                .attr( 'src', this.href );
                            
              if( !response_element.hasClass( 'resized' ) )
              {
                response_element
                  .addClass( 'resized' )
                  .css( 'height', $( '#main' ).height() - 60 );
              }
            }
          )

        $( '.optional legend input[type=checkbox]', query_form )
          .die( 'change' )
          .live
          (
            'change',
            function( event )
            {
              var fieldset = $( this ).parents( 'fieldset' );

              this.checked
                ? fieldset.addClass( 'expanded' )
                : fieldset.removeClass( 'expanded' );
            }
          );

        query_form
          .die( 'submit' )
          .live
          (
            'submit',
            function( event )
            {
              var form_map = {};
              var form_values = [];
              var all_form_values = query_form.formToArray();

              for( var i = 0; i < all_form_values.length; i++ )
              {
                if( !all_form_values[i].value || 0 === all_form_values[i].value.length )
                {
                  continue;
                }

                var name_parts = all_form_values[i].name.split( '.' );
                if( 1 < name_parts.length && !form_map[name_parts[0]] )
                {
                  console.debug( 'skip "' + all_form_values[i].name + '", parent missing' );
                  continue;
                }

                form_map[all_form_values[i].name] = all_form_values[i].value;
                form_values.push( all_form_values[i] );
              }

              var handler_path = $( '#qt', query_form ).val();
              if( '/' !== handler_path[0] )
              {
                form_values.push( { name : 'qt', value : handler_path.esc() } );
                handler_path = '/select';
              }

              var query_url = window.location.protocol + '//' + window.location.host
                            + core_basepath + handler_path + '?' + $.param( form_values );
                            
              url_element
                .attr( 'href', query_url )
                .text( query_url )
                .trigger( 'change' );
                            
              result_element
                .show();
                            
              return false;
            }
          );

        var fields = 0;
        for( var key in context.params )
        {
          if( 'string' === typeof context.params[key] )
          {
            fields++;
            $( '[name="' + key + '"]', query_form )
              .val( context.params[key] );
          }
        }

        if( 0 !== fields )
        {
          query_form
            .trigger( 'submit' );
        }
      }
    );
  }
);